﻿using System;
using BusinessObject;
using Model;
using System.Web.UI.WebControls;

namespace ViewLayer.Pages.Tasks
{
    public partial class CadastroTask : System.Web.UI.Page
    {
        User user;
        private static string vision = "Task";

        protected void Page_Load(object sender, EventArgs e)
        {
            user = (User)Session["User"];
            if (user == null)
            {
                Response.Redirect("../Account/Login.aspx");
            }
            if (!IsPostBack)
            {
                this.populateDdlHistory(this.ddlHistory);
            }
            if (getHistorySession() != null)
            {
                ddlHistory.Visible = false;
            }
        }

        private void populateDdlHistory(DropDownList ddList)
        {
            ddList.DataSource = HistoryBO.Instance.GetHistoryByProject(Session["Project"] as Project);
            ddList.DataTextField = "Nome";
            ddList.DataValueField = "Id";
            ddList.DataBind();
            ddList.Items.Add(new ListItem("Sem Associação", "0"));

            ddList.SelectedValue = "0";
        }

        protected void btnSaveExit_Click(object sender, EventArgs e)
        {
            Task task = createTask();

            if (task != null)
            {
                TaskBO.Instance.Save(task);
                Response.Redirect("../Tasks/InicioTask.aspx");
            }
            else
            {
                lblHistoryError.Visible = true;
            }
        }

        protected void btnSaveContinue_Click(object sender, EventArgs e)
        {
            Task task = createTask();

            if (task != null)
            {
                TaskBO.Instance.Save(task);
                Response.Redirect("../Tasks/CadastroTask.aspx");
            }
            else
            {
                lblHistoryError.Visible = true;
            }
        }

        private Task createTask()
        {
            Task task = new Task();
            task.Nome = txtNome.Text;
            task.Descricao = txtDescricao.Text;
            task.MelhorCaso = double.Parse(txtMelhorCaso.Text);
            task.CasoMedio = double.Parse(txtMaisProvavel.Text);
            task.PiorCaso = double.Parse(txtPiorCaso.Text);

            if (getHistorySession() != null)
            {
                task.History = getHistorySession();
            }

            else
            {
                if (!ddlHistory.SelectedValue.Equals("0"))
                {
                    task.History = HistoryBO.Instance.GetById(int.Parse(ddlHistory.SelectedItem.Value));
                }
                else
                {
                    task.History = null;
                }

                if (getHistorySession() != null)
                {
                    task.History = getHistorySession();
                }
            }

            task.Project = (Project)Session["Project"];

            return task;
        }

        private History getHistorySession()
        {
            return (History)Session["History"];
        }

        protected void btnVoltar_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/InicioTask.aspx");
        }

        protected void lkbCadastrarTarefa_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/InicioTask.aspx");
        }
    }
}